package com.shuwang.order.controller;

import com.shuwang.order.domian.ApplyRespondDomian;
import com.shuwang.order.exception.NotFoundException;
import com.shuwang.order.mapper.OrderManagementMapper;
import com.shuwang.order.model.OrderInfo;
import com.shuwang.order.model.OrderInfoExample;
import com.shuwang.order.model.OrderManagement;
import com.shuwang.order.model.OrderManagementExample;
import com.shuwang.order.until.IDBuilder;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by think on 2017/6/5.
 */
@RestController
@RequestMapping(value = "order")
@Api(value = "management",description = "订单处理 API")
public class OrderManagementController
{
    //打印日志信息
    protected final Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private OrderManagementMapper orderManagementMapper;

    @RequestMapping(value = "management/user/{order_id}/{goods}", method = RequestMethod.POST)
    @ApiOperation(value = "订单处理申请接口",
            notes = "用户在一家商户里选择购物，goods格式    商品ID1，购买数量;商品ID2，购买数量;" +
                    "地址为地址ID"
    )

    public ApplyRespondDomian postOrderController(
            @ApiParam(value = "订单Id") @PathVariable Long order_id,
            @ApiParam(value = "商品") @PathVariable String goods,
            @ApiParam(value = "理由")  @RequestParam(value = "reason", required = false) String reason,
            @ApiParam(value = "图片")  @RequestParam(value = "picture", required = false) String picture
    )
    {   Date today = new Date();
        OrderManagement orderManagement =new OrderManagement();
        Long getId = IDBuilder.CreateAnyId("218");
        orderManagement.setManageId(getId);
        orderManagement.setOrderId(order_id);
        orderManagement.setGoods(goods);
        orderManagement.setReason(reason);
        orderManagement.setPicture(picture);
        orderManagement.setStatus(2103);
        orderManagement.setCreateTime(today);
        orderManagementMapper.insert(orderManagement);

        ApplyRespondDomian applyRespondDomian=new ApplyRespondDomian();
        BeanUtils.copyProperties(orderManagement,applyRespondDomian);
        return  applyRespondDomian;
    }

    @RequestMapping(value = "management/cancel/{manage_id}/{choice}", method = RequestMethod.PUT)
    @ApiOperation(value = "用户取消订单处理接口",
            notes = "输入任意值视为取消"
    )

    public String UsercancelController(
            @ApiParam(value = "订单处理ID") @PathVariable Long manage_id,
            @ApiParam(value = "取消状态") @PathVariable String choice
    )
    {   OrderManagement orderManagement=orderManagementMapper.selectByPrimaryKey(manage_id);

        if (orderManagement==null)
        {
            throw new NotFoundException("未找到任何相关订单信息，请确认订单ID");
        }
        if (!(orderManagement.getStatus().equals(2103))){
            throw new NotFoundException("只有在订单处理中才能取消申请！");
        }
        String request=new String();
        if (choice!=null)
        {request="您的订单取消成功";
            orderManagement.setStatus(2201);
            orderManagementMapper.updateByPrimaryKeySelective(orderManagement);}
        else {request="您的订单取消失败";}
        return  request;}
    @RequestMapping(value = "management/refuse/{manage_id}/{feedback}/{choice}", method = RequestMethod.PUT)
    @ApiOperation(value = "商户拒绝订单处理接口",
            notes = "拒绝状态输入任意值视为拒绝"
    )

    public String ShopRefuseController(
            @ApiParam(value = "订单处理ID") @PathVariable Long manage_id,
            @ApiParam(value = "反馈意见") @PathVariable String feedback,
            @ApiParam(value = "拒绝状态") @PathVariable String choice
    )
    {   OrderManagement orderManagement=orderManagementMapper.selectByPrimaryKey(manage_id);

        if (orderManagement==null)
        {
            throw new NotFoundException("未找到任何相关订单信息，请确认订单ID");
        }
        if (!(orderManagement.getStatus().equals(2103))){
            throw new NotFoundException("只有在订单处理中才能拒绝申请！");
        }
        String request=new String();
        if (choice!=null)
        {request="您的订单拒绝成功";
            orderManagement.setStatus(2202);
            orderManagement.setFeedback(feedback);
            orderManagementMapper.updateByPrimaryKeySelective(orderManagement);}
        else {request="您的订单拒绝失败";}
        return  request;}

    @RequestMapping(value = "management/manage/{manage_id}/{feedback}/{choice}", method = RequestMethod.PUT)
    @ApiOperation(value = "用户订单处理中接口",
            notes = "处理中状态输入任意值视为处理中"
    )
    public String ShopManageController(
            @ApiParam(value = "订单处理ID") @PathVariable Long manage_id,
            @ApiParam(value = "反馈意见") @PathVariable String feedback,
            @ApiParam(value = "处理状态") @PathVariable String choice
    )
    {   OrderManagement orderManagement=orderManagementMapper.selectByPrimaryKey(manage_id);

        if (orderManagement==null)
        {
            throw new NotFoundException("未找到任何相关订单信息，请确认订单ID");
        }
        if (!(orderManagement.getStatus().equals(2103))){
            throw new NotFoundException("只有在订单处理中才能拒绝申请！");
        }
        StringBuilder feedbackrequest=new StringBuilder();
//        feedbackrequest.append()
        String request=new String();
        if (choice!=null)
        {request="您的订单已成功处理";
            orderManagement.setStatus(2204);
            orderManagement.setFeedback(feedback);
            orderManagementMapper.updateByPrimaryKeySelective(orderManagement);}
        else {request="您的订单未成功处理";}
        return  request;}


    @RequestMapping(value = "management/complete/{manage_id}/{feedback}/{choice}", method = RequestMethod.PUT)
    @ApiOperation(value = "用户完成订单处理接口",
            notes = "完成状态输入任意值视为已完成"
    )
    public String ManageCompleteController(
            @ApiParam(value = "订单处理ID") @PathVariable Long manage_id,
            @ApiParam(value = "反馈意见") @PathVariable String feedback,
            @ApiParam(value = "完成状态") @PathVariable String choice
    )
    {   OrderManagement orderManagement=orderManagementMapper.selectByPrimaryKey(manage_id);

        if (orderManagement==null)
        {
            throw new NotFoundException("未找到任何相关订单信息，请确认订单ID");
        }
        if (!(orderManagement.getStatus().equals(2103))){
            throw new NotFoundException("只有在订单处理中才能拒绝申请！");
        }
        String request=new String();
        if (choice!=null)
        {request="您的订单已成功处理";
            orderManagement.setStatus(2204);

            orderManagement.setFeedback(feedback);
            orderManagementMapper.updateByPrimaryKeySelective(orderManagement);}
        else {request="您的订单未成功处理";}
        return  request;}



    @RequestMapping(value = "management/user/{manage_id}", method = RequestMethod.GET)
    @ApiOperation(value = "用户查询订单处理接口",
            notes = "根据MANAGE_ID查询该订单处理的所有信息"
    )
    public OrderManagement UserViewManageController(
            @ApiParam(value = "订单处理ID") @PathVariable Long manage_id
    )
    {   OrderManagement orderManagement=orderManagementMapper.selectByPrimaryKey(manage_id);

        if (orderManagement==null)
        {
            throw new NotFoundException("未找到任何相关订单信息，请确认订单ID");
        }
        return  orderManagement;}





}
